﻿using System.Collections.Generic;
using System.Threading.Tasks;
using XCI.Core;
using XCI.Windows;
using XCI.Sys.Filter;
using XCI.Sys.Model;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统参数服务
    /// </summary>
    public class SysParamService
    {
        private const string RootUrl = "/api/sys/param";
        public static SysParamService Instance { get; } = new SysParamService();

        /// <summary>
        /// 新建参数对象
        /// </summary>
        public async Task<SysParam> NewAsync()
        {
            return await Task.FromResult(new SysParam());
        }

        /// <summary>
        /// 复制参数对象
        /// </summary>
        /// <param name="id">源对象主键</param>
        public async Task CopyAsync(string id)
        {
            var model = await SelectByIdAsync(id);
            model.Id = null;
            model.Spell = null;
            model.Code += " - 复制";
            model.Name += " - 复制";
            await SaveAsync(true,model);
        }

        /// <summary>
        /// 保存参数
        /// </summary>
        /// <param name="created">是否新增</param>
        /// <param name="model">参数对象</param>
        /// <returns>成功返回true</returns>
        public async Task<BoolResult> SaveAsync(bool created, SysParam model)
        {
            return await App.Client.PostAsBoolResultAsync($"{RootUrl}/{(created ? "insert" : "update")}", model);
        }

        /// <summary>
        /// 删除参数
        /// </summary>
        /// <param name="ids">主键字符串,多个主键之间逗号隔开</param>
        /// <returns>成功返回true</returns>
        public async Task DeleteAsync(string ids)
        {
            await App.Client.PostAsync($"{RootUrl}/delete", new { ids });
        }

        /// <summary>
        /// 根据主键获取参数对象
        /// </summary>
        /// <param name="id">参数主键</param>
        public async Task<SysParam> SelectByIdAsync(string id)
        {
            return await App.Client.PostAsync<SysParam>($"{RootUrl}/selectById", new { id });
        }

        /// <summary>
        /// 根据参数编码获取参数对象
        /// </summary>
        /// <param name="code">参数编码</param>
        /// <returns>成功返回参数对象</returns>
        public async Task<SysParam> SelectByCodeAsync(string code)
        {
            return await App.Client.PostAsync<SysParam>($"{RootUrl}/selectByCode", new { code });
        }

        /// <summary>
        /// 根据主键获取参数对象
        /// </summary>
        /// <param name="args">编辑参数</param>
        public Task<SysParam> GetOrCreateAsync(EditFormArgs args)
        {
            if (args == null || args.Created) return NewAsync();
            return SelectByIdAsync(args.Id);
        }

        /// <summary>
        /// 根据编码查询单个参数值
        /// </summary>
        /// <param name="code">参数编码</param>
        /// <param name="defaultValue">如果没有找到指定编码的参数,则返回此默认值</param>
        /// <returns>返回指定编码的参数值</returns>
        public async Task<string> SelectValueByCode(string code, string defaultValue = "")
        {
            return await App.Client.PostAsync<string>($"{RootUrl}/getByCodeFromCache", new { code, defaultValue });
        }

        /// <summary>
        /// 查询参数分页列表
        /// </summary>
        /// <param name="filter">过滤对象</param>
        public async Task<PagingList<SysParam>> SelectPageListAsync(SysParamFilter filter)
        {
            return await App.Client.PostAsync<PagingList<SysParam>>($"{RootUrl}/selectPageList", filter);
        }

        /// <summary>
        /// 查询参数列表
        /// </summary>
        /// <param name="filter">过滤条件</param>
        public async Task<List<SysParam>> SelectListAsync(SysParamFilter filter)
        {
            return await App.Client.PostAsync<List<SysParam>>($"{RootUrl}/selectList", filter);
        }

        /// <summary>
        /// 导出参数列表
        /// </summary>
        /// <param name="filePath">保存路径</param>
        /// <param name="filter">过滤条件</param>
        public async Task ExportAsync(string filePath, SysParamFilter filter)
        {
            await App.Client.DownloadAsync($"{RootUrl}/export", filePath, filter);
        }

        /// <summary>
        /// 刷新服务端参数缓存
        /// </summary>
        public async Task RefreshCacheAsync()
        {
            await App.Client.PostAsync($"{RootUrl}/refreshCache");
        }
    }
}
